﻿#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <cctype>
#include <sstream>

//c++ 17
std::vector<std::string> splitString(const std::string& s) {
    std::vector<std::string> tokens;
    auto start = s.begin();
    auto end = std::find_if(start, s.end(), ::isspace);//isspace判断空格（' '）、水平制表符（'\t'）、换行符（'\n'）、垂直制表符（'\v'）、换页符（'\f'）和回车符（'\r'）

    while (end != s.end()) {
        tokens.emplace_back(start, end);
        start = std::find_if(end, s.end(), [](char c) { return !::isspace(c); });
        end = std::find_if(start, s.end(), ::isspace);
    }

    if (start != s.end()) {
        tokens.emplace_back(start, end);
    }

    return tokens;
}

void textAnalysisDemo() {
    std::string text = "The quick brown fox jumps over the lazy dog. "
        "The dog didn't react at all. The fox was happy.";

    // 1. 分割字符串为单词
    auto words = splitString(text);

    // 2. 转换为小写
    std::transform(words.begin(), words.end(), words.begin(),
        [](std::string word) {
            std::transform(word.begin(), word.end(), word.begin(), ::tolower);
            return word;
        });

    // 3. 移除标点符号
    for (auto& word : words) {
        word.erase(std::remove_if(word.begin(), word.end(), ::ispunct), word.end());
    }

    // 4. 统计词频
	std::map<std::string, int> wordCount;//将出现的频存储在map中，并++统计数量
    for (const auto& word : words) {
        if (!word.empty()) {
            ++wordCount[word];
        }
    }

    // 5. 按频率排序
    std::vector<std::pair<std::string, int>> sortedWords(wordCount.begin(), wordCount.end());
    std::sort(sortedWords.begin(), sortedWords.end(),
        [](const auto& a, const auto& b) { return a.second > b.second; });

    // 打印结果
    std::cout << "Word frequency analysis:\n";
    for (const auto& [word, count] : sortedWords) {
        std::cout << word << ": " << count << "\n";
    }

    // 6. 查找最长单词
    auto longestWord = std::max_element(words.begin(), words.end(),
        [](const std::string& a, const std::string& b) {
            return a.length() < b.length();
        });

    if (longestWord != words.end()) {
        std::cout << "\nLongest word: " << *longestWord
            << " (" << longestWord->length() << " letters)\n";
    }
}

int main() {
    textAnalysisDemo();
    return 0;
}